package mymac.com.viewpager_;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private TextView textView;
    //定义一个存ImageView的数组
    private List<ImageView> imgList;
    //定义一个数组存储图片的ID
    private int[] imageIDArr = {
            R.drawable.a1,
            R.drawable.a2,
            R.drawable.a3
    };
    //定义一个数组存储显示图片的信息
    private String[] imageInfoArr = {
            "星空美景",
            "植物美景",
            "晚霞美景"
    };
    //定义适配器
    private MyViewPagerAdapter pagerAdapter = new MyViewPagerAdapter();;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        textView = (TextView) findViewById(R.id.textView);
        //加载数据
        loadData();
    }

    private void loadData() {
        imgList = new ArrayList<ImageView>();
        for (int i=0; i<imageIDArr.length; i++){
            ImageView imageView = new ImageView(this);
            //优化图片的加载
            InputStream in = this.getResources().openRawResource(imageIDArr[i]);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = false;
            options.inSampleSize = 1;
            Bitmap bm = BitmapFactory.decodeStream(in,null,options);
            imageView.setImageBitmap(bm);
            imgList.add(imageView);
        }
        /**
         * 给viewPager设置适配器
         */
        viewPager.setAdapter(pagerAdapter);
        textView.setText(imageInfoArr[0]);
        /**
         * 给viewPager设置监听
         */
        viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
    }

    /**
     * viewPager监听
     */
    private class MyOnPageChangeListener implements ViewPager.OnPageChangeListener{

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            //动态设置引导页的标题
            textView.setText(imageInfoArr[position]);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    /**
     * viewPager适配器
     */
    private class MyViewPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return imgList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(imgList.get(position));
            return imgList.get(position);
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(imgList.get(position));
        }
    }
}
